package svc

import (
	"github.com/zeromicro/go-zero/core/bloom"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go_zero_proj/shortener/internal/config"
	"go_zero_proj/shortener/model"
	"go_zero_proj/shortener/sequence"
)

type ServiceContext struct {
	Config            config.Config
	ShortUrlModel     model.ShortUrlMapModel
	Sequence          sequence.Sequence
	ShortUrlBlackList map[string]struct{}
	Filter            *bloom.Filter
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.ShortUrlDB.DSN)
	m := make(map[string]struct{}, len(c.ShortUrlBlackList))
	for _, v := range c.ShortUrlBlackList {
		m[v] = struct{}{}
	}
	// 初始化布隆过滤器
	// 初始化 redisBitSet

	// 加载已有的短链接数据
	return &ServiceContext{
		Config:            c,
		Sequence:          sequence.NewMySQL(c.Sequence.DSN),
		ShortUrlModel:     model.NewShortUrlMapModel(conn, c.CacheRedis),
		ShortUrlBlackList: m,
	}

}
